﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DCS.ComLib.Config;
using VietThuongLMS.Services;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Utils;
using System.Web.Security;
using VietThuongLMS.Areas.Admin.Models;

namespace VietThuongLMS.Areas.Admin.Controllers
{

    public class DiemDanhController : Controller
    {
        //
        // GET: /Admin/DiemDanh/
        //[Authorize(Roles = "Admin")]
        //[HttpGet, ValidateInput(false)]
        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Index(int maLopHoc)
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            DiemDanhViewModel DiemDanhList = DiemDanhService.GetDiemDanhList(999999, page, maLopHoc);

            //phân trang
            Pagination pagination = new Pagination(true, Url.Content("~"), "Admin", "DiemDanh", "Index", "?maLopHoc="+maLopHoc+"&p=", DiemDanhList.Total, page, 999999, "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(DiemDanhList);
        }

        ////
        //// GET: /Admin/DiemDanh/Details/5
        //[Authorize(Roles = "Admin")]
        //public ActionResult Detail(int id, int maLopHoc)
        //{
        //    LMSDataEntities context = new LMSDataEntities();
        //    DiemDanhMoRongModel viewModel = new DiemDanhMoRongModel();

        //    viewModel.Pc = context.DiemDanhs.Join(context.LopHocs,
        //        pc => pc.MaLopHoc,
        //        lh => maLopHoc,
        //        (pc, lh) => pc).Where(pc => pc.ID == id).FirstOrDefault();

        //    return View(viewModel);
        //}

        
        ////
        //// GET: /Admin/DiemDanh/Edit/5
        //[Authorize(Roles = "Admin")]
        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Edit(int id, int maLopHoc)
        {
            BangDiemDanh bdd = DiemDanhService.GetDiemDanhById(id, maLopHoc);
            return View(bdd);
        }


        //// POST: /Admin/DiemDanh/Edit/5
        //[Authorize(Roles = "Admin")]
        [Authorize(Roles = "Admin,NhanVien")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(BangDiemDanh bdd, int maLopHoc)
        {
            try
            {
                // TODO: Add update logic here
                int id = -1;
                DiemDanhService.Edit(bdd, ref id);

                //return View(lh);
                //return RedirectToAction("Index", "DiemDanh", new { area = "Admin", @maLopHoc = bdd.HocVien_LopReference.Value.MaLopHoc });
                return RedirectToAction("Index", "DiemDanh", new { area = "Admin", @maLopHoc = maLopHoc });
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DiemDanhController::Edit", ex.StackTrace, ex.Message);
                return View(bdd);
                //return RedirectToAction("View", "DiemDanh", new { area = "Admin", @maLopHoc = pc.MaLopHoc });
            }
        }

        
    }
}
